package com.my.rabbitmq.two;

import com.my.utils.RabbitMQUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @author 林允
 * @version 1.0
 * @description: TODO
 * @date 2023/11/24 21:29
 */
// 工作线程
public class Worker01 {

    // 队列名称
    public final static String QUEUE_NAME = "hello";

    // 接收消息
    public static void main(String[] args) {

        // 接收消息时会执行的内容 参数：消费者的名称 消息体
        DeliverCallback deliverCallback = (consumerTag, message)->{
            System.out.println("consumerTag = " + consumerTag);
            byte[] body = message.getBody();
            System.out.println("new String(body) = " + new String(body));
        };

        // 消息接收时被取消时 执行 参数：消费者的名称
        CancelCallback cancelCallback = (consumerTag) ->{
            System.out.println("consumerTag = " + consumerTag);
        };
        try {
            Channel channel = RabbitMQUtils.getChannel();
            System.out.println("C1等待消息");
            channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);;
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
